package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.dto.MemberAddrAddDTO;
import cn.com.dcsgo.dto.MemberAddrUpdateDTO;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dcsgo.domain.MemberAddr;
import cn.com.dcsgo.service.MemberAddrService;
import cn.com.dcsgo.mapper.MemberAddrMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 针对表【member_addr(用户配送地址)】的数据库操作Service实现
 *
 * @author Dcsgo
 * @since 2025-10-30 15:57:19
 */
@Slf4j
@Service
@CacheConfig(cacheNames = "cn.com.dcsgo.service.impl.MemberAddrServiceImpl")
public class MemberAddrServiceImpl extends ServiceImpl<MemberAddrMapper, MemberAddr>
        implements MemberAddrService {

    @Override
    @Cacheable(key = "#openid")
    public List<MemberAddr> getListByOpenid(String openid) {
        return baseMapper.selectList(
                new LambdaQueryWrapper<MemberAddr>()
                        .eq(MemberAddr::getOpenId, openid)
                        .eq(MemberAddr::getStatus, 1)
                        .orderByDesc(MemberAddr::getCommonAddr, MemberAddr::getCreateTime)
        );
    }

    @Override
    @CacheEvict(key = "#openid")
    @Transactional(rollbackFor = Throwable.class)
    public boolean updateDefaultAddr(String openid, Long addrId) {
        MemberAddr memberAddr = new MemberAddr();
        memberAddr.setCommonAddr(0);
        // 将原来地默认地址改为非默认地址
        boolean updated = update(memberAddr,
                new LambdaQueryWrapper<MemberAddr>()
                        .eq(MemberAddr::getOpenId, openid)
                        .eq(MemberAddr::getStatus, 1)
                        .eq(MemberAddr::getCommonAddr, 1)
        );
        log.debug("Dcsgo:updateDefaultAddr:是否成功更新原来默认的地址:{}", updated);
        // 将指定的地址改为默认地址
        memberAddr.setCommonAddr(1);
        memberAddr.setAddrId(addrId);
        return updateById(memberAddr);
    }

    @Override
    @CacheEvict(key = "#openid")
    @Transactional(rollbackFor = Throwable.class)
    public boolean save(MemberAddrAddDTO addrAddDTO, String openid) {
        MemberAddr saveData = new MemberAddr();
        BeanUtil.copyProperties(addrAddDTO, saveData);
        saveData.setOpenId(openid);
        return save(saveData);
    }

    @Override
    @Cacheable(key = "#addrId")
    public MemberAddr getById(Long addrId) {
        return super.getById(addrId);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(key = "#updateDTO.addrId"),
            @CacheEvict(key = "#openid")
    })
    public boolean update(MemberAddrUpdateDTO updateDTO, String openid) {
        MemberAddr updateData = new MemberAddr();
        BeanUtil.copyProperties(updateDTO, updateData);
        return updateById(updateData);
    }

    @Override
    @Caching(
            evict = {
                    @CacheEvict(key = "#addrId"),
                    @CacheEvict(key = "#openid")
            }
    )
    public boolean removeById(Long addrId, String openid) {
        return removeById(addrId);
    }
}




